package com.jingan.nursinghome.regulatory.select.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.model.Qfinfo;
import com.jingan.nursinghome.regulatory.model.Topic;
import com.jingan.nursinghome.regulatory.select.KHttp;
import com.jingan.nursinghome.regulatory.select.timu.MicrometerTableOSDialog2;
import com.jingan.nursinghome.regulatory.select.timu.MicrometerTableOSDialog22;
import com.jingan.nursinghome.regulatory.view.TriangelView;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;

/**
 * 千分表
 * Created by 彭治铭 on 2018/7/15.
 */
public class MyAdapter2 extends RecyclerView.Adapter {
    BaseActivity activity;
    private List<Qfinfo> models;
    String jlyID;

    public Activity getActivity() {
        return activity;
    }

    public MyAdapter2(BaseActivity activity, List<Qfinfo> models, String jlyID) {
        this.activity = activity;
        this.models = models;
        this.jlyID = jlyID;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView txt_date;//日期
        TextView txt_time;//时间
        TextView txt_people;//巡检人
        TextView getScore;//得分
        TriangelView taringe;//標號
        TextView txt_delay;//詳情

        public MyViewHolder(View itemView) {
            super(itemView);
            txt_date = itemView.findViewById(R.id.txt_date);
            txt_time = itemView.findViewById(R.id.txt_time);
            getScore = itemView.findViewById(R.id.getScore);
            txt_people = itemView.findViewById(R.id.txt_people);
            taringe = itemView.findViewById(R.id.taringe);
            txt_delay = itemView.findViewById(R.id.txt_delay);
        }
    }

    class EmptyViewHolder extends RecyclerView.ViewHolder {
        public EmptyViewHolder(View itemView) {
            super(itemView);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == viewTypeEmpty) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_view, parent, false);
            return new EmptyViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_business2, parent, false);
            return new MyViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            if (position % 2 == 0) {
                Animation animation_alpha = AnimationUtils.loadAnimation(getActivity(), R.anim.zebra_right_in_without_alpha);
                holder.itemView.startAnimation(animation_alpha);
            } else {
                Animation animation_alpha = AnimationUtils.loadAnimation(getActivity(), R.anim.zebra_left_in_without_alpha);
                holder.itemView.startAnimation(animation_alpha);
            }
            final Qfinfo qfinfo = models.get(position);
            myViewHolder.txt_date.setText(qfinfo.getDate());//日期
            myViewHolder.txt_time.setText(qfinfo.getTime());//时间
            myViewHolder.txt_people.setText(qfinfo.getAccount());//巡检人

            myViewHolder.getScore.setText(qfinfo.getZhenggainum() + "个");//整改项
            myViewHolder.taringe.setIndex(position + 1 + "");//標號
            myViewHolder.txt_delay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //詳情
                    qfinfo.setJly(MicrometerTable.getInstance().getJlyName(jlyID));//巡检机构名称
                    MicrometerTable.getInstance().setQfinfo(qfinfo);
                    //MicrometerTableDetailsDialog.getInstance(getBaseActivity(), qfinfo.getId());
                    KHttp.INSTANCE.Topic(activity, qfinfo.getId(), new BaseListenner<ArrayList<Topic>>() {
                        @Override
                        public void onResult(ArrayList<Topic> topics) {
                            //MicrometerTableOSDialog2.getInstance(activity, topics, qfinfo.getId()).show();
                            new MicrometerTableOSDialog22(activity, topics, qfinfo.getId(), true, true).show();
                        }
                    });
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return models.size() > 0 ? models.size() : 1;
    }

    int viewTypeEmpty = -1;

    @Override
    public int getItemViewType(int position) {
        if (models.size() <= 0) {
            return viewTypeEmpty;
        }
        return position;
    }
}
